/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import model.ProductModel;
import util.DataUtil;

/**
 *
 * @author ThanhToan
 */
public class ProductDAO {
    public static ArrayList<ProductModel> getAllProduct(){
        ArrayList<ProductModel> proList = new ArrayList<ProductModel>();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllProduct()}");
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                ProductModel proModel = new ProductModel();
                proModel.setId(rs.getString("ID"));
                proModel.setCateName(rs.getString("CategoryName"));
                proModel.setProName(rs.getString("ProductName"));
                proModel.setImage(rs.getString("Image"));
                proModel.setProDes(rs.getString("Description"));
                proModel.setPrice(rs.getString("Price"));
                proList.add(proModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proList;
    }

    public static ArrayList<ProductModel> getAllProductByCateID(String cateID){
        ArrayList<ProductModel> proList = new ArrayList<ProductModel>();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllProductByCateID(?)}");
            csmt.setString("cateID", cateID);
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                ProductModel proModel = new ProductModel();
                proModel.setId(rs.getString("ID"));
                proModel.setCateName(rs.getString("CategoryName"));
                proModel.setProName(rs.getString("ProductName"));
                proModel.setImage(rs.getString("Image"));
                proModel.setProDes(rs.getString("Description"));
                proModel.setPrice(rs.getString("Price"));
                proList.add(proModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proList;
    }

    public static ProductModel getProductByProID(String proID){
        ProductModel proModel = new ProductModel();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllProductByProID(?)}");
            csmt.setString("proID", proID);
            ResultSet rs = DataUtil.executeStore(csmt);
            if(rs.next()){
                proModel.setId(rs.getString("ID"));
                proModel.setCateName(rs.getString("CategoryName"));
                proModel.setProName(rs.getString("ProductName"));
                proModel.setImage(rs.getString("Image"));
                proModel.setProDes(rs.getString("Description"));
                proModel.setPrice(rs.getString("Price"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proModel;
    }

    public static boolean insertProduct(ProductModel proModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_InsertProduct(?,?,?,?,?)}");
            csmt.setString("cateID", proModel.getCateID());
            csmt.setString("proName", proModel.getProName());
            csmt.setNString("image", proModel.getImage());
            csmt.setNString("des", proModel.getProDes());
            csmt.setNString("price", proModel.getPrice());
            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean updateProduct(ProductModel proModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_InsertProduct(?,?,?,?,?,?)}");
            csmt.setString("id", proModel.getId());
            csmt.setString("cateID", proModel.getCateID());
            csmt.setString("proName", proModel.getProName());
            csmt.setNString("image", proModel.getImage());
            csmt.setNString("des", proModel.getProDes());
            csmt.setNString("price", proModel.getPrice());
            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
